﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using CLFramework.Windows.Forms;
using System.Xml;
//using StockMonitor.Helper;

namespace StockMonitor.UserControls
{
  //  [ModuleContract("851CC145-12CA-4A6F-B041-9985D718D71F", "查询(&Q)", "产品查询")]
    public partial class UCMarketQuery : UserControl
    {


        public UCMarketQuery()
        {
         //   frmLogin.Login();
            InitializeComponent();
        }

        public string Title
        {
            get
            {
                return "直接调用恒生接口下单";
            }

        }


        public UCMarketQuery(XmlNode node)
            : this()
        {
            InitializeComponent();
        }
        HsGateway hS = new HsGateway();
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {


                var ret = hS.F105查询股票市场(int.Parse(txtbranch.Text), this.txtcode.Text);
            this.propertyGrid1.SelectedObject = ret.Count > 0 ? ret[0] : null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.ToString()+Environment.NewLine );
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {


                var ret = hS.F105查询股票市场(int.Parse(txtbranch.Text), this.txtcode.Text);
           // var ret2 = HSCOM.hS.F400查询行情(ret[0].exchange_type, this.textBox2.Text);
            this.propertyGrid1.SelectedObject = ret;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.ToString()+Environment.NewLine );
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //F302委托确认(int op_branch_no, string password, int branch_no, int fund_account, string exchange_type, string stock_account, string stock_code, double entrust_amount, double entrust_price, string entrust_prop, string entrust_bs, string bank_no, string client_rights)
            try {
                var res = hS.F302委托确认(int.Parse(txtbranch.Text), txtpwd.Text, int.Parse(txtbranch.Text), int.Parse(txtUsr.Text), "2", "", this.txtcode.Text, int.Parse(this.txtamount.Text), double.Parse(this.txtprice.Text), "", "1", "", "");
            this.propertyGrid1.SelectedObject = res;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.ToString()+Environment.NewLine );
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try {
                var ret = hS.F403查询股票(int.Parse(txtbranch.Text), int.Parse(txtbranch.Text), int.Parse(txtUsr.Text), txtpwd.Text, "2", "", "", "", "", int.Parse(txtbranch.Text), "");
            this.dataGridView1.DataSource = ret;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.ToString()+Environment.NewLine );
            }
            //foreach (var item in ret)
            //{
            //    this.propertyGrid1.SelectedObject = ret;
            //    MessageBox.Show(item.ToString());
            //}
               
            
            

        }

        private void button5_Click(object sender, EventArgs e)
        {

            /*
             * 
        ///<param name="op_branch_no">操作员分支代码</param>
        ///<param name="branch_no">操作员分支代码</param>
        ///<param name="fund_account">资金账号</param>
        ///<param name="password">交易密码</param>
        ///<param name="exchange_type">交易类别，如果不为空格，查确定的，为空，查所有。</param>
        ///<param name="stock_account">证券账号</param>
        ///<param name="stock_code">证券代码，如果为空格，查所有，不为空，查确定的，此时exchange_type必须不为空</param>
        ///<param name="locate_entrust_no">指定委托号，查指定委托号的委托，且request_num为1，且前台在收到应答后要检查返回的委托的委托号是否与要求查询的一致，此时exchange_type和stock_code要为空串。</param>
        ///<param name="query_type">查询类别，是否查委托类型为撤单的委托，0全部(默认)；1不查委托类型为撤单的委托</param>
        ///<param name="query_direction">查询方向</param>
        ///<param name="sort_direction">排序方式 '0' 正常 '1'倒序</param>
        ///<param name="request_num">请求行数，不送按50行处理，超过系统指定值（1000行）按系统指定值（1000行）处理</param>
        ///<param name="position_str">定位串</param>
        ///<param name="action_in">操作确认，1，则只查询可以撤单的委托；0及其他，则查询全部委托</param>
             * 
             */
            //(int op_branch_no, int branch_no, int fund_account, string password, string exchange_type, string stock_account, string stock_code, int locate_entrust_no, string query_type, string query_direction, string sort_direction, int request_num, string position_str, int action_in)
            try
            {
                var res = hS.F401查询委托(int.Parse(txtbranch.Text), int.Parse(txtbranch.Text), int.Parse(txtUsr.Text), txtpwd.Text, "", "", "", 21, "0", "", "0", 1000, "", 0);// 8.0, "", "0","1", "", "");
            //this.propertyGrid1.SelectedObject = res;
            this.dataGridView1.DataSource = res;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.ToString()+Environment.NewLine );
            }
            //foreach (var item in res)
            //{
            //    this.propertyGrid1.SelectedObject = item;
            //    MessageBox.Show("");
            //}
        }

        
        private void button6_Click_1(object sender, EventArgs e)
        {
            try
            {


                var res = hS.F402查询成交(1000, 1000, int.Parse(txtUsr.Text), txtpwd.Text, "", "", "", "", "", 1000, "");
                this.dataGridView1.DataSource = res;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.ToString()+Environment.NewLine );
            }
            //foreach (var item in ret)
            //{
            //    this.propertyGrid1.SelectedObject = item;
            //}

        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            hS.Connect(1, txtaddr.Text, int.Parse(txtport.Text));
        }
    }
}
